%     AERO LOAD - программа для нагружения агрегатов самолета распределенной нагрузкой
%     Copyright (C) 2018-2024  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function Write_Config(Work_Directory, Date_String, Load, Input_BDF_name)

global ErrCode;

global software_version;

number_format = '%.10f';

fprintf (1, '\nСоздание конфигурационного файла...                  ');

File_name = ['Aero_Load_' Modify_Symbols(Date_String)];
File_path = [Work_Directory '\' File_name '.alcfg'];

Fout = fopen(File_path, 'wt');

if (Fout < 0)
    fprintf (1, '\nОшибка функции Write_Config: не удалось открыть для записи файл [%s] !\n', File_path);
    ErrCode = -1;
    return;
end

fprintf(Fout, '# AERO LOAD CONFIGURATION FILE\n');
fprintf(Fout, '# DATE: %s\n', Date_String);
fprintf(Fout, '# INPUT BDF NAME: %s\n\n', Input_BDF_name);

fprintf(Fout, 'SOFTWARE_VERSION = %s\n\n', software_version);

fprintf(Fout, 'BASIC_PLANE_DEFINITION = %s\n', Load.Basic_Plane.Definition);

fprintf(Fout, ['Z0 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Basic_Plane.Z0(1), Load.Basic_Plane.Z0(2), Load.Basic_Plane.Z0(3));
fprintf(Fout, ['Z1 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Basic_Plane.Z1(1), Load.Basic_Plane.Z1(2), Load.Basic_Plane.Z1(3));

fprintf(Fout, ['D1 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Basic_Plane.D1(1), Load.Basic_Plane.D1(2), Load.Basic_Plane.D1(3));
fprintf(Fout, ['D2 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Basic_Plane.D2(1), Load.Basic_Plane.D2(2), Load.Basic_Plane.D2(3));
fprintf(Fout, ['D3 = ' number_format ' ' number_format ' ' number_format '\n\n'], Load.Basic_Plane.D3(1), Load.Basic_Plane.D3(2), Load.Basic_Plane.D3(3));

fprintf(Fout, ['A1 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Chord_Surface.A1(1), Load.Chord_Surface.A1(2), Load.Chord_Surface.A1(3));
fprintf(Fout, ['A2 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Chord_Surface.A2(1), Load.Chord_Surface.A2(2), Load.Chord_Surface.A2(3));
fprintf(Fout, ['A3 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Chord_Surface.A3(1), Load.Chord_Surface.A3(2), Load.Chord_Surface.A3(3));
fprintf(Fout, ['A4 = ' number_format ' ' number_format ' ' number_format '\n\n'], Load.Chord_Surface.A4(1), Load.Chord_Surface.A4(2), Load.Chord_Surface.A4(3));

fprintf(Fout, ['PN = ' number_format '\n\n'], Load.Pn);

fprintf(Fout, 'SPAN_LOAD_LAW = %s\n\n', Load.Span_Load.Law);

fprintf(Fout, 'SPAN_TRAPEZOID_ZD_TYPE = %s\n', Load.Span_Load.Trapezoid_Zd_Type);
fprintf(Fout, ['SPAN_TRAPEZOID_ZD_VALUE = ' number_format '\n'], Load.Span_Load.Trapezoid_Zd_Value);
fprintf(Fout, ['SPAN_TRAPEZOID_ZD_LOCATION = ' number_format ' ' number_format ' ' number_format '\n\n'], Load.Span_Load.Trapezoid_Zd_Location(1), Load.Span_Load.Trapezoid_Zd_Location(2), Load.Span_Load.Trapezoid_Zd_Location(3));

fprintf(Fout, 'LOAD_DIAGRAM\n');

if (~isempty(Load.Span_Load.Load_Diagram_Initial_Data))
    for i=1:size(Load.Span_Load.Load_Diagram_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Span_Load.Load_Diagram_Initial_Data(i,1), Load.Span_Load.Load_Diagram_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste load diagram here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_PRESSURE_LAW = %s\n\n', Load.Chord_Pressure.Law);

fprintf(Fout, 'CHORD_TRAPEZOID_XD_TYPE = %s\n', Load.Chord_Pressure.Trapezoid_Xd_Type);
fprintf(Fout, ['CHORD_TRAPEZOID_XD_VALUE = ' number_format '\n'], Load.Chord_Pressure.Trapezoid_Xd_Value);
fprintf(Fout, ['CHORD_TRAPEZOID_XD_LOCATION = ' number_format ' ' number_format ' ' number_format '\n'], Load.Chord_Pressure.Trapezoid_Xd_Location(1), Load.Chord_Pressure.Trapezoid_Xd_Location(2), Load.Chord_Pressure.Trapezoid_Xd_Location(3));
fprintf(Fout, 'CHORD_TRAPEZOID_XD_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Trapezoid_Xd_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Trapezoid_Xd_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Trapezoid_Xd_Distr_Initial_Data(i,1), Load.Chord_Pressure.Trapezoid_Xd_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_MODEL_X_P2_P1_TYPE = %s\n', Load.Chord_Pressure.Model_X_p2_p1_Type);
fprintf(Fout, ['CHORD_MODEL_X_P2_P1_VALUE = ' number_format '\n'], Load.Chord_Pressure.Model_X_p2_p1_Value);
fprintf(Fout, 'CHORD_MODEL_X_P2_P1_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Model_X_p2_p1_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Model_X_p2_p1_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Model_X_p2_p1_Distr_Initial_Data(i,1), Load.Chord_Pressure.Model_X_p2_p1_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_MODEL_X_P3_P1_TYPE = %s\n', Load.Chord_Pressure.Model_X_p3_p1_Type);
fprintf(Fout, ['CHORD_MODEL_X_P3_P1_VALUE = ' number_format '\n'], Load.Chord_Pressure.Model_X_p3_p1_Value);
fprintf(Fout, 'CHORD_MODEL_X_P3_P1_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Model_X_p3_p1_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Model_X_p3_p1_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Model_X_p3_p1_Distr_Initial_Data(i,1), Load.Chord_Pressure.Model_X_p3_p1_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_MODEL_X_P4_P1_TYPE = %s\n', Load.Chord_Pressure.Model_X_p4_p1_Type);
fprintf(Fout, ['CHORD_MODEL_X_P4_P1_VALUE = ' number_format '\n'], Load.Chord_Pressure.Model_X_p4_p1_Value);
fprintf(Fout, 'CHORD_MODEL_X_P4_P1_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Model_X_p4_p1_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Model_X_p4_p1_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Model_X_p4_p1_Distr_Initial_Data(i,1), Load.Chord_Pressure.Model_X_p4_p1_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_MODEL_X_F_TYPE = %s\n', Load.Chord_Pressure.Model_X_f_Type);
fprintf(Fout, ['CHORD_MODEL_X_F_VALUE = ' number_format '\n'], Load.Chord_Pressure.Model_X_f_Value);
fprintf(Fout, 'CHORD_MODEL_X_F_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Model_X_f_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Model_X_f_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Model_X_f_Distr_Initial_Data(i,1), Load.Chord_Pressure.Model_X_f_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_MODEL_X_F1_TYPE = %s\n', Load.Chord_Pressure.Model_X_f1_Type);
fprintf(Fout, ['CHORD_MODEL_X_F1_VALUE = ' number_format '\n'], Load.Chord_Pressure.Model_X_f1_Value);
fprintf(Fout, 'CHORD_MODEL_X_F1_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Model_X_f1_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Model_X_f1_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Model_X_f1_Distr_Initial_Data(i,1), Load.Chord_Pressure.Model_X_f1_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_MODEL_X_F2_TYPE = %s\n', Load.Chord_Pressure.Model_X_f2_Type);
fprintf(Fout, ['CHORD_MODEL_X_F2_VALUE = ' number_format '\n'], Load.Chord_Pressure.Model_X_f2_Value);
fprintf(Fout, 'CHORD_MODEL_X_F2_DISTRIBUTION\n');

if (~isempty(Load.Chord_Pressure.Model_X_f2_Distr_Initial_Data))
    for i=1:size(Load.Chord_Pressure.Model_X_f2_Distr_Initial_Data,1)
        fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.Model_X_f2_Distr_Initial_Data(i,1), Load.Chord_Pressure.Model_X_f2_Distr_Initial_Data(i,2));
    end
else
    fprintf(Fout, '# Paste distribution here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'CHORD_USER_MODEL\n');

if (~isempty(Load.Chord_Pressure.User_Model_Initial_Data))
    for i=1:size(Load.Chord_Pressure.User_Model_Initial_Data.Span_Coord,1)
        fprintf(Fout, ['SPAN ' number_format '\n'], Load.Chord_Pressure.User_Model_Initial_Data.Span_Coord(i));
        for j=1:size(Load.Chord_Pressure.User_Model_Initial_Data.Pressure_Data{i},1)
            fprintf(Fout, ['' number_format '\t' number_format '\n'], Load.Chord_Pressure.User_Model_Initial_Data.Pressure_Data{i}(j,1), Load.Chord_Pressure.User_Model_Initial_Data.Pressure_Data{i}(j,2));
        end
    end
else
    fprintf(Fout, '# Paste user model data here\n');
end
fprintf(Fout, '\n');

fprintf(Fout, 'SPAN_LOAD_EXTRAP_0 = %s\n', Load.Extrapolation.Span_Load_0);
fprintf(Fout, 'SPAN_LOAD_EXTRAP_1 = %s\n\n', Load.Extrapolation.Span_Load_1);

fprintf(Fout, 'CHORD_PRESSURE_EXTRAP_0 = %s\n', Load.Extrapolation.Chord_Pressure_0);
fprintf(Fout, 'CHORD_PRESSURE_EXTRAP_1 = %s\n\n', Load.Extrapolation.Chord_Pressure_1);

fprintf(Fout, 'USER_MODEL_EXTRAP_0 = %s\n', Load.Extrapolation.User_Model_0);
fprintf(Fout, 'USER_MODEL_EXTRAP_1 = %s\n\n', Load.Extrapolation.User_Model_1);

fprintf(Fout, 'TRAPEZOID_XD_EXTRAP_0 = %s\n', Load.Extrapolation.Trapezoid_Xd_0);
fprintf(Fout, 'TRAPEZOID_XD_EXTRAP_1 = %s\n\n', Load.Extrapolation.Trapezoid_Xd_1);

fprintf(Fout, 'MODEL_X_P2_P1_EXTRAP_0 = %s\n', Load.Extrapolation.Model_X_p2_p1_0);
fprintf(Fout, 'MODEL_X_P2_P1_EXTRAP_1 = %s\n\n', Load.Extrapolation.Model_X_p2_p1_1);

fprintf(Fout, 'MODEL_X_P3_P1_EXTRAP_0 = %s\n', Load.Extrapolation.Model_X_p3_p1_0);
fprintf(Fout, 'MODEL_X_P3_P1_EXTRAP_1 = %s\n\n', Load.Extrapolation.Model_X_p3_p1_1);

fprintf(Fout, 'MODEL_X_P4_P1_EXTRAP_0 = %s\n', Load.Extrapolation.Model_X_p4_p1_0);
fprintf(Fout, 'MODEL_X_P4_P1_EXTRAP_1 = %s\n\n', Load.Extrapolation.Model_X_p4_p1_1);

fprintf(Fout, 'MODEL_X_F_EXTRAP_0 = %s\n', Load.Extrapolation.Model_X_f_0);
fprintf(Fout, 'MODEL_X_F_EXTRAP_1 = %s\n\n', Load.Extrapolation.Model_X_f_1);

fprintf(Fout, 'MODEL_X_F1_EXTRAP_0 = %s\n', Load.Extrapolation.Model_X_f1_0);
fprintf(Fout, 'MODEL_X_F1_EXTRAP_1 = %s\n\n', Load.Extrapolation.Model_X_f1_1);

fprintf(Fout, 'MODEL_X_F2_EXTRAP_0 = %s\n', Load.Extrapolation.Model_X_f2_0);
fprintf(Fout, 'MODEL_X_F2_EXTRAP_1 = %s\n\n', Load.Extrapolation.Model_X_f2_1);

fprintf(Fout, ['SUMMATION_POINT = ' number_format ' ' number_format ' ' number_format '\n'], Load.Summation_Point(1), Load.Summation_Point(2), Load.Summation_Point(3));
fprintf(Fout, ['COORD_ORIGIN = ' number_format ' ' number_format ' ' number_format '\n'], Load.Coord_Origin(1), Load.Coord_Origin(2), Load.Coord_Origin(3));
fprintf(Fout, ['POINT_ON_AXIS_3 = ' number_format ' ' number_format ' ' number_format '\n'], Load.Point_On_Axis_3(1), Load.Point_On_Axis_3(2), Load.Point_On_Axis_3(3));
fprintf(Fout, ['POINT_ON_PLANE_13 = ' number_format ' ' number_format ' ' number_format '\n\n'], Load.Point_On_Plane_13(1), Load.Point_On_Plane_13(2), Load.Point_On_Plane_13(3));

fprintf(Fout, ['CORRECTION_COEFFICIENT = ' number_format '\n\n'], Load.Correction_Coefficient);

fprintf(Fout, 'DISTRIBUTED_LOAD_MODE = %s\n', Load.Distributed_Load_Mode);
fprintf(Fout, 'VECTOR_FIELD_COMPONENT = %i\n', Load.Vector_Field_Component);
fprintf(Fout, 'FIELD_NAME = %s\n', Load.Field_Name);

fclose(Fout);

fprintf (1, 'OK!\n');

fprintf (1, '\n    Файл [%s.alcfg] создан успешно!\n', File_name);

